﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Models;

namespace Data
{
    public class TagsData:DataBase
    {
       
        public void AddTag(string tagName)
        {

            ExecuteNonQuery("AddTag",new List<SqlParameter>
                                         {
                                             new SqlParameter("@Name", tagName)
                                         });

        }
        public void DeleteTag(int id)
        {
            ExecuteNonQuery("DeleteTag",new List<SqlParameter>
                                            {
                                                new SqlParameter("@Id",id)
                                            });
        }

        public void UpdateTag(Tag tag)
        {
            ExecuteNonQuery("UpdateTag",new List<SqlParameter>
                                            {
                                                new SqlParameter("@Id", tag.Id),
                                                new SqlParameter("@Name", tag.Name)
                                            });
        }

        public List<Tag> GetTags(int id = 0,string name = null)
        {
            var parametrs = new List<SqlParameter>
                                {
                                    new SqlParameter("@Id",id),
                                    new SqlParameter("@Name",name)
                                };

            var dataTable = ExecuteDataTable("GetTags", parametrs);

            if (dataTable.Rows.Count > 0)
            {
                var tagsList = new List<Tag>();

                foreach (DataRow row in dataTable.Rows)
                {
                    tagsList.Add(new Tag
                                     {
                                         Id = Convert.ToInt32(row["id"]),
                                         Name = Convert.ToString(row["name"])
                                     });
                }

                return tagsList;
            }

            return null;

        }
    }
}